
#include "CGameSceneManager.h"
#include "CGameObjectLoader.h"
#include "irrString.h"

namespace game
{
	namespace scene
	{
		CGameSceneManager::CGameSceneManager(IDeviceManager* deviceManager) 
			:	IGameObject(NULL, deviceManager->getSceneManager(), deviceManager->getVideoHandler()),
				CurrentScene(0),
				DeviceManager(0),
				Creators(0)
		{
			
			this->DeviceManager = deviceManager;
			
			
			this->DeviceManager->grab();

			this->Creators = new irr::core::map<irr::core::stringc, delegateGameSceneCreator*>;
		}

		CGameSceneManager::~CGameSceneManager()
		{
            if(Creators)
				delete Creators;

			if(DeviceManager)
				DeviceManager->drop();
		}

		IGameScene* CGameSceneManager::getCurrentGameScene()          
		{ 
			return CurrentScene;
		}

		void CGameSceneManager::setCurrentGameScene(IGameScene* value)
		{
			if(CurrentScene)
			{
				if(!CurrentScene->ChangeToNextScene.IsNull())
				{
					CurrentScene->ChangeToNextScene -= NewDelegate(this, &CGameSceneManager::onChangeScene);
					CurrentScene->drop();
				}
			}

			CurrentScene = value;

			if(CurrentScene)
			{
				CurrentScene->establishScene();
				CurrentScene->ChangeToNextScene += NewDelegate(this, &CGameSceneManager::onChangeScene);
			}

		}

		bool CGameSceneManager::onUserAction(SUserAction action)
		{
			/*bool result = false;

			if(CurrentScene)
			{
				result =  CurrentScene->onUserAction(action);
			}

			return result;*/
			return false;

		}

		bool CGameSceneManager::onNetworkRequest(void* e)
		{
			/*bool result = false;

			if(CurrentScene)
			{
				result =  CurrentScene->onNetworkRequest();
			}

			return result;*/
			return false;
		}

		void CGameSceneManager::onChangeScene(EventArgs e)
		{
		}

		void CGameSceneManager::registerGameSceneType(const c8* typeName, delegateGameSceneCreator* gameSceneCreator)
		{
			irr::core::stringc typeNameStr = typeName;

			typeNameStr.make_upper();

			this->Creators->insert(typeNameStr, gameSceneCreator);
		}

		IGameScene* CGameSceneManager::getGameScene(const c8* fileName)
		{
			IGameScene* result = NULL;
			
			CGameObjectLoader* loader = new CGameObjectLoader(this);
			result = loader->getGameScene(fileName, this->Creators);
			
			loader->drop();

			return result;
		}


		IDynamicObject* CGameSceneManager::getDynamicObject(const c8* fileName, EGAME_OBJECT_TYPE type)
		{
			IDynamicObject* result = NULL;
			
			CGameObjectLoader* loader =  new CGameObjectLoader(this);
			result = loader->getDynamicObject(fileName, type);
			
			loader->drop();

			return result;
		}

		//! Load location object from xml file. If you want load world just use this func, but set type "world"
		ILocationObject* CGameSceneManager::getLocationObject(const c8* fileName)
		{
			ILocationObject* result = NULL;
			
			CGameObjectLoader* loader =  new CGameObjectLoader(this);
			result = loader->getLocationObject(fileName);
			
			loader->drop();

			return result;
		}


		irr::scene::ISceneManager* CGameSceneManager::getSceneManager()
		{
			return this->DeviceManager->getSceneManager();
		}

		video::IVideoHandler* CGameSceneManager::getVideoHandler()
		{
			return this->DeviceManager->getVideoHandler();
		}


		//! Not worked in this class.
		void CGameSceneManager::addToScene()
		{
		}

		//! Not worked in this class. 
		void CGameSceneManager::removeFromScene()
		{
		}

		//! Not worked in this class.
		void CGameSceneManager::setPosition(const irr::core::vector3df& value)
		{
		}

		//! Creator
		IGameSceneManager* createGameSceneManager(IDeviceManager* deviceManager)
		{
			IGameSceneManager* result = new CGameSceneManager(deviceManager);

			return result;
		}

	}
}
